package com.raos.zkconfig.handwritten.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 手写 ZK 配置中心之springboot本地配置演示
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2022/9/5 22:06
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // field.set(bean, value)
    /**
     * 其实源码中要解析到@Value注解上的value，比如把value拿到之后，发现是${age}，所以要把${age}变成 age，
     * 然后根据age从env#getProperty("age")中获取对应的值
     */
    // 入口在哪里? BeanPostProcessor  AutowiredAnnotationBeanPostProcessor#inject  获取到age对应的Field字段
    // ageField#set(clazz, environment.getProperty("age"))
    @Value("${age}")
    private Integer age;

    @Value("${hobby}")
    private String hobby;

    @RequestMapping("/config-value")
    public String config() {
        return "age: " + this.age + ", hobby: " + this.hobby;
    }

    @Resource    // 依赖注入Environment对象实例
    private Environment environment;

    @RequestMapping("/conf-env")
    public String confEnv() {
        // this.environment.getProperty("age")其实是通过属性配置源获取到对应的数据内容
        return "age: " + this.environment.getProperty("age")
                + ", hobby: " + this.environment.getProperty("hobby");
    }

}
